﻿
@using IOA.MES.DataAccess.Enum
@{
    ViewBag.Title = "CTM报表";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
    var startTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 0, 0) ? DateTime.Today.AddDays(-1).AddHours(20) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(8) : DateTime.Today.AddHours(20));
    var endTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 0, 0) ? DateTime.Today.AddHours(8) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(20) : DateTime.Today.AddDays(1).AddHours(8));
}
@section Contents{
    <link href="~/res/kendo/styles/kendo.common.min.css" rel="stylesheet" />
    <link href="~/res/kendo/styles/kendo.material.min.css" rel="stylesheet" />
    <style>
        .layui-form-label {
            width: auto;
        }

        #txtOrderNo, #txtWorkOrderNo {
            width: 100%;
            height: 30px;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkShop">车间：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode">@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.OrderNo">订单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtOrderNo" placeholder="请输入订单号" value="" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderNo">工单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtWorkOrderNo" placeholder="请输入工单号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderDate">生产时间：</label>
                        <div class="layui-input-inline" style="width:310px;">
                            <input type="text" id="txtRangeTime" placeholder="请选择工单时间" value="@(startTime.ToString("yyyy-MM-dd HH:mm:ss") + " 至 " + endTime.ToString("yyyy-MM-dd HH:mm:ss"))" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <a id="btnExport" class="layui-btn layui-btn-normal layuiadmin-btn-list" target="_blank" href="javascript:void(0)">
                            <i class="layui-icon layui-icon-download-circle layuiadmin-button-btn"></i>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script src="/res/kendo/kendo.all.min.js"></script>
    <script src="/res/kendo/js/cultures/kendo.culture.zh-CN.min.js"></script>
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                laydate.render({
                    elem: '#txtRangeTime'
                    , range: '至'
                    , type: 'datetime'
                    , done: function (value, date, endDate) {
                        queryNos(0);
                        queryNos(1);
                    }
                });
                form.on("select(selWorkshops)", function (data) {
                    queryNos(0);
                    queryNos(1);
                });

                var txtOrderNos = $("#txtOrderNo").kendoAutoComplete({
                    dataSource: vm.$data.nos,
                    filter: "contains",
                    placeholder: "选择订单号"
                }).focus(function () {
                    txtOrderNos.search("");
                }).data("kendoAutoComplete"),
                    txtWorkOrderNos = $("#txtWorkOrderNo").kendoAutoComplete({
                        dataSource: vm.$data.nos,
                        filter: "contains",
                        placeholder: "选择工单号"
                    }).focus(function () {
                        txtWorkOrderNos.search("");
                    }).data("kendoAutoComplete");

                function queryNos(noType) {
                    var rangeTime = $("#txtRangeTime").val().trim() == '' ? new Array() : $("#txtRangeTime").val().trim().split(' 至 ');
                    var startTime = rangeTime.length <= 0 ? null : rangeTime[0],
                        endTime = rangeTime.length <= 0 ? null : rangeTime[1];

                    $.ajax({
                        url: "/Common/QueryOrderNos?noType=" + noType + "&workshopCode=" + $("#selWorkshops").val().trim() + "&key=" + "&startTime=" + startTime + "&endTime=" + endTime,
                        type: "GET",
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            if (noType == 0) {
                                txtOrderNos.setDataSource(new kendo.data.DataSource({
                                    data: res.Data
                                }));
                            } else {
                                txtWorkOrderNos.setDataSource(new kendo.data.DataSource({
                                    data: res.Data
                                }));
                            }
                        }
                    });
                }
                var options = {
                    elem: '#table'
                    , url: '/Report/QueryCTMS'
                    , method: "post"
                    , where: { workshopCode: "", orderNo: "", workOrderNo: "", startTime: null, endTime: null }
                    , page: false
                    , limit: 10000
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60, fixed: "left" }
                        , { field: 'OrderNo', title: '订单号', width: 150 }
                        , { field: 'BatteryFactory', title: '厂商', width: 150 }
                        , { field: 'BatterySpecs', title: '型号', width: 200 }
                        , { field: 'CellEfficiency', title: '效率', width: 80 }
                        , { field: 'TheoreticalPower', title: "理论功率", width: 100 }
                        , { field: 'Quantity', title: "测试数", width: 100 }
                        , { field: 'AveragePower', title: "平均功率", width: 100 }
                        , { field: 'CTM', title: "CTM", width: 100 }
                        , { field: 'Desc', title: "描述", minWidth: 100 }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    options.where.workshopCode = $("#selWorkshops").val().trim();
                    options.where.orderNo = $("#txtOrderNo").val().trim();
                    options.where.workOrderNo = $("#txtWorkOrderNo").val().trim();
                    var rangeTime = $("#txtRangeTime").val().trim() == '' ? new Array() : $("#txtRangeTime").val().trim().split(' 至 ');
                    options.where.startTime = rangeTime.length <= 0 ? null : rangeTime[0];
                    options.where.endTime = rangeTime.length <= 0 ? null : rangeTime[1];

                    $("#btnExport").attr("href", "/Report/ExportCTMS?workshopCode=" + options.where.workshopCode + "&orderNo=" + options.where.orderNo + "&workOrderNo=" + options.where.workOrderNo + "&startTime=" + options.where.startTime + "&endTime=" + options.where.endTime);

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }

                $("#btnExport").click(function () {
                    var rangeTime = $("#txtRangeTime").val().trim() == '' ? new Array() : $("#txtRangeTime").val().trim().split(' 至 ');
                    var startTime = rangeTime.length <= 0 ? null : rangeTime[0];
                    var endTime = rangeTime.length <= 0 ? null : rangeTime[1];
                    var workshopCode = $("#selWorkshops").val().trim(),
                        orderNo = $("#txtOrderNo").val().trim(),
                        workOrderNo = $("#txtWorkOrderNo").val().trim();
                    $(this).attr("href", "/Report/ExportCTMS?workshopCode=" + workshopCode + "&orderNo=" + orderNo + "&workOrderNo=" + workOrderNo + "&startTime=" + startTime + "&endTime=" + endTime);
                });
            });
        }
    </script>
}
